package cn.matezk.lianlian.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.json.JSONObject;
import cn.matezk.lianlian.dao.DeviceDao;
import cn.matezk.lianlian.framework.RedisMutexLock;
import cn.matezk.lianlian.service.IMiniAppService;
import cn.matezk.lianlian.service.ITencentService;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.iotcloud.v20210408.IotcloudClient;
import com.tencentcloudapi.iotexplorer.v20190423.IotexplorerClient;
import com.tencentcloudapi.iotexplorer.v20190423.models.*;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class TencentService implements ITencentService {
    @Autowired
    private WxMpService wxMpService;
    @Autowired
    private WxMaService wxMaService;
    @Autowired
    private IMiniAppService miniAppService;
    @Autowired
    private IotexplorerClient explorerClient;
    @Autowired
    private IotcloudClient cloudClient;
    @Autowired
    private RedisMutexLock mutexLock;
    @Autowired
    private DeviceDao deviceDao;

    protected ControlDeviceDataResponse deviceProperty(String productId, String deviceName, JSONObject property) throws TencentCloudSDKException {
        ControlDeviceDataRequest request = new ControlDeviceDataRequest();
        request.setProductId(productId);
        request.setDeviceName(deviceName);
        request.setData(property.toString());
        return explorerClient.ControlDeviceData(request);
    }

    public CallDeviceActionSyncResponse deviceActionSync(String productId, String deviceName, String action, String params) throws TencentCloudSDKException {
        CallDeviceActionSyncRequest request = new CallDeviceActionSyncRequest();
        request.setProductId(productId);
        request.setDeviceName(deviceName);
        request.setActionId(action);
        request.setInputParams(params);
        return explorerClient.CallDeviceActionSync(request);
    }

    public com.tencentcloudapi.iotcloud.v20210408.models.DescribeDeviceResponse describeDevice(String productId, String deviceName) throws TencentCloudSDKException {
        com.tencentcloudapi.iotcloud.v20210408.models.DescribeDeviceRequest request = new com.tencentcloudapi.iotcloud.v20210408.models.DescribeDeviceRequest();
        request.setProductId(productId);
        request.setDeviceName(deviceName);
        return cloudClient.DescribeDevice(request);
    }

    @Override
    public DescribeDeviceDataResponse property(String productId, String deviceName) throws TencentCloudSDKException {
        DescribeDeviceDataRequest request = new DescribeDeviceDataRequest();
        request.setProductId(productId);
        request.setDeviceName(deviceName);
        return explorerClient.DescribeDeviceData(request);
    }

    @Override
    public ControlDeviceDataResponse controlDevice(String productId, String deviceName, String data) throws TencentCloudSDKException {
        ControlDeviceDataRequest request = new ControlDeviceDataRequest();
        request.setProductId(productId);
        request.setDeviceName(deviceName);
        request.setData(data);
        return explorerClient.ControlDeviceData(request);
    }
}
